package com.lovo.psc.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lovo.psc.dto.OrderDto;
import com.lovo.psc.entity.PscOrder;
import com.lovo.psc.entity.PscTriumph;
import com.lovo.psc.service.IPscOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/supplyOrder")
@Api(tags = "供货订单模块——————李鑫002版本")
public class PscOrderController {
    @Autowired
    IPscOrderService service;

    @GetMapping("findIdOrder/{supplyCode}")
    @ApiOperation(value = "按照供应商查询订单")
    public List<OrderDto> findIdOrder(@PathVariable("supplyCode") Integer supplyCode){
        List<OrderDto> pscOrderList = service.findIdOrder(supplyCode);
        for (int i =0;i<pscOrderList.size();i++){
            if (pscOrderList.get(i).getState().equals("0")){
                pscOrderList.get(i).setState("未付款");
            }else if (pscOrderList.get(i).getState().equals("1")){
                pscOrderList.get(i).setState("已付款");
            }else if (pscOrderList.get(i).getState().equals("2")){
                pscOrderList.get(i).setState("已完成");
            }else if (pscOrderList.get(i).getState().equals("3")){
                pscOrderList.get(i).setState("正在退货");
            }
        }
        return pscOrderList;
    }

    @GetMapping("findIdByStateOrder/{supplyCode}/{supplyState}")
    @ApiOperation(value = "按照供应商和货物状态查询订单")
    public List<OrderDto> findIdByStateOrder(@PathVariable("supplyCode") Integer supplyCode,
                                             @PathVariable("supplyState") String supplyState){
        List<OrderDto> pscOrderList = service.findIdByStateOrder(supplyCode,supplyState);
        for (int i =0;i<pscOrderList.size();i++){
            if (pscOrderList.get(i).getState().equals("0")){
                pscOrderList.get(i).setState("未付款");
            }else if (pscOrderList.get(i).getState().equals("1")){
                pscOrderList.get(i).setState("已付款");
            }else if (pscOrderList.get(i).getState().equals("2")){
                pscOrderList.get(i).setState("已完成");
            }else if (pscOrderList.get(i).getState().equals("3")){
                pscOrderList.get(i).setState("正在退货");
            }
        }
        return pscOrderList;
    }

    @PostMapping("AddOrder")
    @ApiOperation(value = "添加供货订单")
    public String AddOrder(PscTriumph pscTriumph){
        if (service.AddOrder(pscTriumph)=="ok"){
            return "添加成功";
        }
        return "添加失败";
    }

    @DeleteMapping("DelOrder/{delId}")
    @ApiOperation(value = "删除供货订单")
    public String DelOrder(@PathVariable("delId") String delId){
        if (delId!=null){
            service.DelOrder(delId);
            return "删除成功";
        }
        return "删除失败";
    }

    @GetMapping("findRetreatOrder/{supplyCode}")
    @ApiOperation(value = "查询退货订单")
    public List<OrderDto> findRetreatOrder(@PathVariable("supplyCode") Integer supplyCode){
        List<OrderDto> retreatList = service.findIdByStateOrder(supplyCode,"3");
        if (retreatList.size()!=0&&retreatList != null){
            for (int r =0;r<retreatList.size();r++){
                if (retreatList.get(r).getState().equals("0")){
                    retreatList.get(r).setState("未付款");
                }else if (retreatList.get(r).getState().equals("1")){
                    retreatList.get(r).setState("已付款");
                }else if (retreatList.get(r).getState().equals("2")){
                    retreatList.get(r).setState("已完成");
                }else if (retreatList.get(r).getState().equals("3")){
                    retreatList.get(r).setState("正在退货");
                }
            }
            return retreatList;
        }
        return null;
    }

    @ApiOperation(value = "修改订单状态")
    @PutMapping("updateState/{supplyCode}/{supplyState}")
    public void updateState(@PathVariable("supplyCode") String supplyCode,@PathVariable("supplyState") String supplyState){
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("orderNumber",supplyCode);
        PscOrder pscOrder = new PscOrder();
        pscOrder.setOrdernumber(supplyCode);
        pscOrder.setState(supplyState);
        service.update(pscOrder,wrapper);
    }
}
